package enterprise.manage.web.filter;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import enterprise.manage.entity.RequestLog; 

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * User: libofeng
 * Date: 2009-8-11
 * Time: 11:07:26
 * Description:强制站点使用https
 */
public class HttpsFilter implements Filter {
    private Boolean enabled = false;

    public void init(FilterConfig filterConfig) throws ServletException {

        enabled = "true".equalsIgnoreCase(filterConfig.getInitParameter("enabled"));
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String url = request.getRequestURL().toString();
        if (enabled && url.startsWith("http://")) {
            String targetUrl = url.substring("http".length());
            ServletContext s = ((HttpServletRequest) servletRequest).getSession().getServletContext();
            ApplicationContext a = WebApplicationContextUtils.getWebApplicationContext(s);
//            LogRequestService logRequestService = (LogRequestService) a.getBean("logRequestService");
            RequestLog requestLog = new RequestLog();
            if ("aspx".equals(url.substring(url.length() - 4, url.length()))) {
                System.out.println("插入访问日志");
                String username = request.getParameter("username");
                String ip = request.getRemoteAddr();
                if (username == null || "".equals(username)) {
                    username = (String) request.getSession().getAttribute("username");
                }
                requestLog.setIp(ip);
                requestLog.setUserName(username);
                requestLog.setRequestUrl(url);
//                logRequestService.saveLogReqeust(requestLog);
            }
            StringBuffer outBuffer = new StringBuffer();
            targetUrl = targetUrl == null ? "" : targetUrl.replace("\r", "\\r").replace("\n", "\\n");
            outBuffer.append("<script type=\"text/javascript\" language=\"Javascript\">");
            outBuffer.append("location.replace(");
            outBuffer.append("\"h\"+\"t\"+\"t\"+\"p\"+\"s\"+\"");
            outBuffer.append(targetUrl);
            outBuffer.append("\");");
            outBuffer.append("</script>");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(outBuffer);
            response.getWriter().flush();

        } else {
            ServletContext s = ((HttpServletRequest) servletRequest).getSession().getServletContext();
            ApplicationContext a = WebApplicationContextUtils.getWebApplicationContext(s);
//            LogRequestService logRequestService = (LogRequestService) a.getBean("logRequestService");
            RequestLog requestLog = new RequestLog();
            if ("aspx".equals(url.substring(url.length() - 4, url.length()))) {
                System.out.println("插入访问日志");
                String username = request.getParameter("username");
                String ip = request.getRemoteAddr();
                if (username == null || "".equals(username)) {
                    username = (String) request.getSession().getAttribute("username");
                }
                requestLog.setIp(ip);
                requestLog.setUserName(username);
                requestLog.setRequestUrl(url);
//                logRequestService.saveLogReqeust(requestLog);
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    public void destroy() {
    }
}

